<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn">添加分类</el-button>
      </div>

      <!-- 文章分类 -->
      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="cate_name" label="分类名称"></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
          <!-- scope对象：{ row: 行对象 } -->
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="updateCateBtnFn(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCateFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>

    <!-- 添加文章分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="30%" @close="dialogCloseFn">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10" placeholder="1-10位的非空字符"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15" placeholder="1-15位的字母或数字"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getArtCateListAPI, saveArtCateAPI, updateArtCateAPI, delArtCateAPI } from '@/api/index'
export default {

  data () {
    return {
      // 文章分类数组
      cateList: [],
      // 控制对话框的显示与隐藏
      dialogVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      // true为编辑状态，false为新增状态
      isEdit: false,
      // 保存正在要编辑的数据id值
      editId: ''
    }
  },

  name: 'ArtCate',

  methods: {
    async getArtCateFn () {
      const res = await getArtCateListAPI()
      this.cateList = res.data.data
    },

    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },

    // 对话框显示按钮
    addCateShowDialogBtnFn () {
      this.isEdit = true
      this.editId = ''
      this.dialogVisible = true
    },

    // 对话框确认点击按钮，消失时保存文章分类接口
    confirmFn () {
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          if (!this.isEdit) {
            // 要修改分类
            this.addForm.id = this.editId
            const { data: res } = await updateArtCateAPI(this.addForm)
            if (res.code !== 0) return this.$message.error(res.message)
            this.$message.success(res.message)
            this.$refs.addRef.resetFields()
          } else {
            // 要新增类名
            const { data: res } = await saveArtCateAPI(this.addForm)
            if (res.code !== 0) return this.$message.error(res.message)
            this.$message.success(res.message)
          }
          this.getArtCateFn()
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },

    // 对话框取消点击按钮
    cancelFn () {
      this.dialogVisible = false
    },

    // 弹框关闭时的回调
    dialogCloseFn () {
      this.$refs.addRef.resetFields()
    },

    // 修改点击按钮事件
    updateCateBtnFn (obj) {
      // 先做数据回显
      this.isEdit = false
      this.editId = obj.id
      this.dialogVisible = true
      this.addForm.cate_name = obj.cate_name
      this.addForm.cate_alias = obj.cate_alias
    },

    // 删除文章分类的点击事件
    async delCateFn (obj) {
      const { data: res } = await delArtCateAPI(obj.id)
      console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getArtCateFn()
    }

  },

  created () {
    // 获取文章分类
    this.getArtCateFn()
  }

}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
/deep/.el-dialog__wrapperl {
  width: 50%;
}
// /deep/.el-table__row:hover {
//   background-color: blue;
// }
</style>
